/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.boapi.shop;

import org.boapi.core.IChecknumber;

/**
 * International Standard Music Number [ISMN] since 01.01.2008 
 * with 13 digits. Before 01.01.2008 ISMN have M prefix. 
 * @author Sebastian Ritter
 * @version 2.0.0.0
 */
public final class ISMN implements IChecknumber {
  /**
   * Delegate mostly to GTIN
   */
  private GTIN delegateObject;
  /**
   * Construct
   */
  public ISMN () {
    this.delegateObject = new GTIN ();
  }
  /**
   * Set the ISMN. It accept new and old format
   * @param ismn in array element zero
   */
  public void setCompleteValue (final String ismn) {
	this.setCompleteValue(new String []{ismn});
  }
  /**
   * Set the ISMN. It accept new and old format
   * @param ismn in array element zero
   */
  public void setCompleteValue (final String[] ismn) {
	if (ismn != null && ismn.length>0) {
	  if ('M' == ismn[0].charAt(0) || 'm' == ismn[0].charAt(0)) {
		ismn[0] = "9790"+ismn[0].substring(1); // ohne Bindestrich wird beim parsen sowieso weggeschnitten
	  }
	}
    this.delegateObject.setCompleteValue (ismn);
    if (!(this.delegateObject.getNumber().startsWith ("9790"))) {
      throw new IllegalArgumentException ("Number is not an ISMN, because it starts not with 979-0");
    }
  }

  /**
   * Is the checknumber correct
   * @returns boolean true if checknumber ok
   */
  public final boolean isChecknumberCorrect () {
    return this.delegateObject.isChecknumberCorrect ();
  }
  /**
   * Set the checknumber
   * @param digit the checknumber 
   */
  protected void setChecknumber (final char digit) {
    this.delegateObject.setChecknumber (digit);
  }

  /**
   * Return the checknumber
   * @return the check digit / char
   */
  protected char getChecknumber () {
    return this.delegateObject.getChecknumber ();
  }

  /**
   * The number of ISBN
   * @return String the unformatted number
   */
  public String getNumber () {
    return this.delegateObject.getNumber();
  }

}
